var content = document.querySelector('.content');
var container = document.querySelector('.container');
var subBtn = document.getElementById('subBtn');
var backBtn = document.getElementById('backBtn');
var tableBody = document.querySelector('.grade-table tbody');
var currentIndex = 1;
var data = [];
var tbody = document.querySelector(".grade-table tbody");
var backtest = [];
var backtestt = [];

document.addEventListener("DOMContentLoaded", function() {

  $.ajax({
    url: '/management/teaservlet/teaSearchCourseStudents',
    method: 'POST',
    success: function(response) {
      data = response.data;
      data.forEach(function(item) {
        var row = document.createElement("tr");
        row.innerHTML = `
          <td>${currentIndex}</td>
          <td id="id_${currentIndex}">${item.Stu_id}</td>
          <td>${item.Stu_name}</td>
          <td>${item.Cou_name}</td>
          <td>${item.Stu_major}</td>
          <td>${item.Stu_class}</td>
          <td><input type="text" id="Score_${currentIndex}" value="${item.Score}"></td>
          <td><input type="text2" id="Sco_Info_${currentIndex}" value="${item.Sco_Info}"></td>
        `;
        tbody.appendChild(row);
        currentIndex++;
      });

      for(let m=1;m<currentIndex;m++){
        var test3 = document.getElementById('id_'+m).textContent;
        console.log(test3);
        var test = document.getElementById('Score_'+m);
        var test2 = document.getElementById('Sco_Info_'+m);
        backtest[m-1] = new Array(3);
        backtest[m-1][0]=test3;
        backtest[m-1][1]=test.value;
        backtest[m-1][2]=test2.value;
      }
      console.log(backtest);
    },
    error: function(xhr, status, error) {
      console.log(error);
    }
  });
});

content.addEventListener('mouseenter', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.5)'; // 将透明度更改为 0.5，使背景色加深
});

content.addEventListener('mouseleave', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.2)'; // 将透明度恢复为 0.2，使背景色变浅
});

backBtn.addEventListener('click', function() {
  var confirmLogout = confirm("确定要返回吗？");
  if (confirmLogout) {
    window.history.back();
  }
  else{

  }
});

subBtn.addEventListener('click', function() {
  var confirmLogout = confirm("确定要提交吗？");
      for(let m=1;m<currentIndex;m++){
        var test3 = document.getElementById('id_'+m).textContent;
        console.log(test3);
        var test = document.getElementById('Score_'+m);
        var test2 = document.getElementById('Sco_Info_'+m);
        var stringin = test3+','+test.value+','+test2.value;
        backtest[m-1] = stringin;
//        backtest[m-1][0]=test3;
//        backtest[m-1][1]=test.value;
//        backtest[m-1][2]=test2.value;
      }
      console.log(backtest);
  if (confirmLogout) {
    $.ajax({
      url: '/management/teaservlet/ScoreToDataBase',
      method: 'POST',
      data: {
        backtest: backtest
      },
      success: function(response) {
        // var data1 = response.data;
        // console.log(data1);
        console.log("success");
        alert("录入成功！");
      },
      error: function(xhr, status, error) {
        console.log(error);
        alert("录入失败！");
      }
    });
  }
  else{

  }
});
